const { resolve } = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
    entry: "./src/js/index.js",
    output: {
        filename: "js/index.js",
        path: resolve(__dirname, "dist"),
    },
    module: {
        rules: [
            // 处理css资源
            {
                test: /\.css$/,
                use: ["style-loader", "css-loader"],
            },
            // 处理less资源
            {
                test: /\.less$/,
                use: ["style-loader", "css-loader", "less-loader"],
            },
            // 处理图片资源
            {
                test: /\.(png|jpg|gif)$/,
                loader: "url-loader",
                options: {
                    limit: 8 * 1024,
                    name: "[hash:10].[ext]",
                    outputPath: "images",
                },
            },
            // 处理html中img图片资源
            {
                test: /\.html$/,
                loader: "html-loader",
                options: {
                    esModule: false,
                },
            },
            // 处理其它资源
            {
                exclude: /\.(css|less|js|png|jpg|gif|html)$/,
                loader: "file-loader",
                options: {
                    name: "[hash:10].[ext]",
                    outputPath: "assets",
                },
            },
        ],
    },
    plugins: [
        // 配置html模板
        new HtmlWebpackPlugin({
            template: "./src/index.html",
        }),
    ],
    mode: "development",

    // 配置开发服务器
    devServer: {
        // 配置打包后的项目路径
        contentBase: resolve(__dirname, "dist"),
        // 启用gzip压缩
        compress: true,
        port: 3000,
        open: true,
    },
};
